package com.example.bangbangxia.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.bangbangxia.service.UserService;
import com.example.bangbangxia.utils.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "用户登陆注册相关接口")
public class LoginController {
    @Autowired
    private UserService userService;


    @ApiOperation("用户登陆接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userPhone",value = "用户手机号",defaultValue = "18475030818"),
            @ApiImplicitParam(name = "userPassword",value = "密码",defaultValue = "123456",required = true)
    })
    @PostMapping(value = "/login")
    public RespBean login(@RequestBody JSONObject requestJson) {
        String userPhone=requestJson.getString("userPhone");
        String userPassword=requestJson.getString("userPassword");
        return userService.login(userPhone, userPassword);
    }

    /**
     * 注册
     * @return
     */
    @ApiOperation("注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userPhone",value = "18475030818",required = true),
            @ApiImplicitParam(name="userPassword",value = "123456",required = true),
            @ApiImplicitParam(name="userPassword2",value = "123456",required = true)
    })
    @PostMapping(value = "/register")
//    String userPhone, String userPassword, String userPassword2
    public RespBean register(@RequestBody JSONObject requestJson) {
        //取值
        String userPhone=requestJson.getString("userPhone");
        String userPassword=requestJson.getString("userPassword");
        String userPassword2=requestJson.getString("userPassword2");
        return userService.register(userPhone, userPassword, userPassword2);
    }

    /**
     * 手机验证码登陆，发送验证码
     * @return
     */
    @ApiOperation("根据手机发送验证码")
    @ApiImplicitParam(name = "userPhone",value = "18475030818",required = true)
    @GetMapping(value = "/loginByPhone/{userPhone}")
    public RespBean loginByPhone(@PathVariable("userPhone") String userPhone){
        //判空
        if (userPhone==null||userPhone==""){
            return RespBean.error("参数不完整");
        }
        return userService.loginByPhone(userPhone);
    }

    /**
     * 验证验证码是否正确
     * @return
     */
    @ApiOperation("验证验证码是否正确")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userPhone",value = "18475030818",required = true),
            @ApiImplicitParam(name = "code",value = "56165",required = true)
    })
    @GetMapping(value = "loginByPhoneCheck/{userPhone}/{code}")
    public RespBean loginByPhoneCheck(@PathVariable("userPhone") String userPhone,@PathVariable(name="code") String code){
        //判空
        if (userPhone==null||userPhone=="" || code==null || code==""){
            return RespBean.error("参数不完整");
        }
        return userService.loginByPhoneCheck(userPhone,code);
    }
}
